<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" th:href="@{/res/css/amazeui.min.css}"/>
    <link rel="stylesheet" th:href="@{/res/css/admin.css}"/>
    <script th:src="@{/js/jquery-1.11.3.min.js}"></script>
    <script th:src="@{/myplugs/js/plugs.js}"></script>
</head>

<style>
    .block{
        width: 400px;
        display: block;
        margin:30px 0;
    }
    .center{
        text-align: center;
    }
    label {
        display: inline-block;
        width: 100px;
        text-align: right;
    }
    input{
        vertical-align: top;
    }
    form{
        position:relative;
        left:35%;
        margin-top: 8%;
    }
    select{
        width: 180px;
    }
    #add{
        margin-right: 5%;
    }
</style>

<body>
<div class="admin-content-body">
    <div class="am-cf am-padding am-padding-bottom-0">
        <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">新增店铺</strong>
            <small></small>
        </div>
    </div>

    <hr>

    <form class="">
        <fieldset>
            <div class="block">
                <label for="name">店&nbsp;&nbsp;铺&nbsp;&nbsp;名:&nbsp;&nbsp;</label>
                <input type="text" id="name">
            </div>

            <div class="block">
                <label for="type">店铺类型:&nbsp;&nbsp;</label>
                <select id="type">
                    <option id="type1" value="amazon">amazon</option>
                    <option id="type2" value="ebay">ebay</option>
                </select>
                <span class="am-form-caret"></span>
            </div>

            <div class="block center">
                <input type="button" id="add" class="am-btn am-btn-primary am-round" value="确认增加"/>
            </div>
        </fieldset>
    </form>
</div>
</body>
<script>
    //id从url得到
    var result = new Object();
    result = getParameter();
    var uid = result["uid"];
    console.log(uid);

    $("#add").click(function () {
        var name = document.getElementById("name").value;
        if ($("#type").val() == "amazon")
            var type = $("#type1").val();
        else
            var type = $("#type2").val();
        console.log(type);
        if(name==""){
            alert("店铺名不能为空");
        }else{
            $.ajax({
                type: "POST",
                url: "/store/addStore",
                dataType: "text",
                data: {uid: uid, name: name, type: type},
                success: function (mydata) {
                    if (mydata == "true") {
                        alert("添加店铺成功");
                        window.location.href="/storeMes?uid=" + uid;
                    } else {
                        alert("该店铺已存在，请重新添加");
                    }
                },
                error: function () {
                    alert("添加店铺请求失败");
                }
            })
        }
    })

    //解析url传值
    function getParameter() {
        var url = location.search;//获取url?后面的内容
        var result = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                result[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return result;
    }
</script>
</html>
